Problem
Sightseeing Plan
Statement
Joisino is planning on touring Takahashi Town. The town is divided into square sections by north-south and east-west lines. We will refer to the section that is the from the west and the from the north as .
Joisino thinks that a touring plan is good if it satisfies the following conditions:
Let be the section where she starts the tour. Then, and hold.
Let be the section where she has lunch. Then, and hold.
Let be the section where she ends the tour. Then, and hold.
By repeatedly moving to the adjacent section (sharing a side), she travels from the starting section to the ending section in the shortest distance, passing the lunch section on the way.
Two touring plans are considered different if at least one of the following is different: the starting section, the lunch section, the ending section, and the sections that are visited on the way. Joisino would like to know how many different good touring plans there are. Find the number of the different good touring plans. Since it may be extremely large, find the count modulo .
Constraints
Input
Input is given from Standard Input in the following format:
Output
Print the number of the different good touring plans, modulo .
Sample
Input #1
1 | 1 1 2 2 3 4 |
Output #1
1 | 10 |
Explanation #1
The starting section will always be , and the lunch section will always be . There are four good touring plans where is the ending section, and six good touring plans where is the ending section. Therefore, the answer is .
Input #2
1 | 1 2 3 4 5 6 |
Output #2
1 | 2346 |
Input #3
1 | 77523 89555 420588 604360 845669 973451 |
Output #3
1 | 137477680 |
标签:组合数学
Translation
给出三个矩形,,。
求从中任意一点出发,经过中任意指定一点,到达中任意一点,且只能向上走或向右走的路径数。
注意:如果两条路径相同,而在中经过的指定点不同,则视为两条不同路径。即,可将一条路径看成二元组,其中是在中指定经过的点,若两者中任意一者不同,则视为两条不同路径。
Solution
挺麻烦的计数。
设为从走到的方案数,那么有。
引理 1:。
证明:考虑所有从到的路径,其一定经过其仅经过一条形如的边。而经过的路径的条数恰好为,于是有。
引理 2:
证明:
由以上可用类似二维前缀和的方法得到
于是我们有了一个计算从到一个矩形中的任意点的路径个数。类似地,枚举两个矩形各四个基准点即可对于任意在两个矩形中间的点计算从到其再到的路径条数。
对于一条路径,其一定进入一次再从出去一次,设入口和出口曼哈顿距离为,则其一定能对应条中指定点不同的路径。而从左边和下边的每个点进入于从上边和右边的每个点出去的贡献都是可以拆开的,于是枚举入点计算贡献,再枚举出点计算贡献。
具体地,
- 对于,从进入的路径每条路贡献均为
- 对于,从进入的路径每条路贡献均为
- 对于,从出去的路径每条路贡献均为
- 对于,从出去的路径每条路贡献均为
附上官方题解
Code
1 |
|